首页>代码>spring mvc+spring+mybatis框架整合开发基于角色到按钮级别的java权限后台管理系统>/rbps Maven Webapp/src/main/java/dingzhen/controller/MenuController.java
package dingzhen.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import dingzhen.entity.Menu;
import dingzhen.entity.Operation;
import dingzhen.service.MenuService;
import dingzhen.service.OperationService;
import dingzhen.util.StringUtil;
import dingzhen.util.WriterUtil;

/**
 *@author: wangq
 *@date: 2015-5-19下午04:22:53
 *@version:
 *@description:
 */
@RequestMapping("menu")
@Controller
public class MenuController {

	private Menu menu;
	private Operation operation;
	@Autowired
	private MenuService<Menu> menuService;
	@Autowired
	private OperationService<Operation> operationService;
	static Logger logger = Logger.getLogger(MenuController.class);
	
	
	@RequestMapping("menuIndex")
	public String index(){
		return "menu";
	}
	
	@RequestMapping("treeGridMenu")
	public void treeGridMenu(HttpServletRequest request,HttpServletResponse response){
		try {
			String parentId=request.getParameter("parentId");
			JSONArray jsonArray = getListByParentId(parentId);
			WriterUtil.write(response, jsonArray.toString());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("菜单展示错误",e);
		}
	}
	
	
	
	
	public JSONArray getListByParentId(String parentId)throws Exception{
		JSONArray jsonArray=this.getTreeGridMenuByParentId(parentId);
		for(int i=0;i<jsonArray.size();i++){
			JSONObject jsonObject=jsonArray.getJSONObject(i);
			if("open".equals(jsonObject.getString("state"))){
				continue;
			}else{
				jsonObject.put("children", getListByParentId(jsonObject.getString("id")));
			}
		}
		return jsonArray;
	}
	
	
	
	public JSONArray getTreeGridMenuByParentId(String parentId)throws Exception{
		JSONArray jsonArray=new JSONArray();
		menu = new Menu();
		menu.setParentId(Integer.parseInt(parentId));
		List<Menu> list = menuService.findMenu(menu);
		for(Menu menu : list){
			JSONObject jsonObject = new JSONObject();
			Integer menuId = menu.getMenuId();
			jsonObject.put("id", menuId);
			jsonObject.put("text", menu.getMenuName());
			jsonObject.put("iconCls", menu.getIconCls());
			jsonObject.put("state", menu.getState());
			jsonObject.put("seq", menu.getSeq());
			jsonObject.put("menuUrl", menu.getMenuUrl());
			jsonObject.put("menuDescription", menu.getMenuDescription());
			
			// 加上该页面菜单下面的按钮
			operation = new Operation();
			operation.setMenuId(menuId);
			List<Operation> operaList = operationService.findOperation(operation);
			if (operaList!=null && operaList.size()>0) {
				String string = "";
				for (Operation o : operaList) {
					string += o.getOperationName() + ",";
				}
				jsonObject.put("operationNames", string.substring(0,string.length()-1));
			} else {
				jsonObject.put("operationNames", "");
			}
			jsonArray.add(jsonObject);
		}
		return jsonArray;
	}
	
	
	
	@RequestMapping("reserveMenu")
	public void reserveMenu(HttpServletRequest request,HttpServletResponse response,Menu menu){
		String menuId = request.getParameter("menuId");
		JSONObject result=new JSONObject();
		try {
			if (StringUtil.isNotEmpty(menuId)) {  //更新操作
				menu.setMenuId(Integer.parseInt(menuId));
				menuService.updateMenu(menu);
			} else {
				String parentId = request.getParameter("parentId");
				menu.setParentId(Integer.parseInt(parentId));
				if (isLeaf(parentId)) {
					// 添加操作
					menuService.addMenu(menu);  
					
					// 更新他上级状态。变成CLOSED
					menu = new Menu();
					menu.setMenuId(Integer.parseInt(parentId));
					menu.setState("closed");
					menuService.updateMenu(menu);
				} else {
					menuService.addMenu(menu);
				}
			}
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("菜单保存错误",e);
			result.put("success", true);
			result.put("errorMsg", "对不起,操作失败!");
		}
		WriterUtil.write(response, result.toString());
	}
	
	
	
	// 判断是不是叶子节点
	public boolean isLeaf(String menuId){
		boolean flag = false;
		try {
			menu = new Menu();
			menu.setParentId(Integer.parseInt(menuId));
			List<Menu> list = menuService.findMenu(menu);
			if (list==null || list.size()==0) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("判断菜单是否叶子节点错误",e);
		}
		return flag;
	}
	
	
	
	
	@RequestMapping("deleteMenu")
	public void deleteMenu(HttpServletRequest request,HttpServletResponse response){
		JSONObject result = new JSONObject();
		try {
			String menuId = request.getParameter("menuId");
			String parentId = request.getParameter("parentId");
			if (!isLeaf(menuId)) {  //不是叶子节点,说明有子菜单,不能删除
				result.put("errorMsg", "该菜单下面有子菜单,不能直接删除");
			} else {
				menu = new Menu();
				menu.setParentId(Integer.parseInt(parentId));
				int sonNum = menuService.countMenu(menu);
				if (sonNum == 1) {  
					// 只有一个孩子,删除该孩子,且把父亲状态置为open
					menu = new Menu();
					menu.setMenuId(Integer.parseInt(parentId));
					menu.setState("open");
					menuService.updateMenu(menu);
					
					menuService.deleteMenu(Integer.parseInt(menuId));
				} else {
					//不只一个孩子,直接删除
					menuService.deleteMenu(Integer.parseInt(menuId));
				}
				result.put("success", true);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("菜单删除错误",e);
			result.put("errorMsg", "对不起,删除失败!");
		}
		WriterUtil.write(response, result.toString());
	}
	
	 

}
最近下载更多
vitos5n  LV9 2023年5月22日
wanglinddad  LV54 2022年4月24日
liujun0104  LV4 2022年3月29日
做你的英雄  LV14 2022年3月14日
Start1  LV15 2022年3月8日
1214955637  LV2 2021年1月2日
泪染珍珠  LV9 2020年11月3日
wangxc87  LV1 2020年8月3日
吴鑫1998  LV9 2020年6月23日
zhangtian1997  LV10 2020年6月7日
最近浏览更多
wddq123 4月3日
暂无贡献等级
WBelong  LV7 2023年12月26日
漫步的海星  LV4 2023年9月21日
飞呀飞呀飞不放  LV7 2023年8月9日
zhy1989wz  LV6 2023年7月6日
1379585889  LV11 2023年6月7日
zhaoqfan  LV2 2023年5月15日
hao2290211  LV1 2023年4月14日
uni-code_0123  LV1 2023年3月23日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友